<script>
	import routingIntercept from './utils/permission.js'
	import {
		setAuthorization,
		getAuthorization,
		removeAuthorization
	} from '@/utils/auth'

	export default {
		globalData: {
			api_key: "dbe1ed7bmk12kdokweld7213b2a622asdc4d12313991bdcx1298d3fasd899baad274c416ce72b7e8",
			serverUrl: 'http://47.117.67.131:5000/zzcloud'
		},
		onLaunch: function() {
			// setAuthorization(JSON.stringify({
			// 	"token": 123456,
			// 	"user_id": 123456
			// }))
			
			// // if (getAuthorization() == '') {
			// // 	routingIntercept()
			// // } else {
			// 	// 不是白名单，有token，判断token是否失效
			// 	let data = {
			// 		"user_id": JSON.parse(getAuthorization()).user_id, //账号（同手机号）
			// 		"token": JSON.parse(getAuthorization()).token, //本地token
			// 		"api_key": getApp().globalData.api_key
			// 	}
			// 	uni.request({
			// 		url: getApp().globalData.serverUrl + '/api/login/checktoken',
			// 		data: JSON.stringify(data),
			// 		method: 'POST',
			// 		contentType: 'application/json',
			// 		complete: async (res) => {
			// 			console.log(res)
			// 			if (res.data.code != '0000') {
			// 				removeAuthorization()
			// 				await routingIntercept()
			// 			}
			// 		},
			// 	})
			// // }
			//#ifdef APP-PLUS    
			plus.runtime.getProperty(plus.runtime.appid, (inf) => {
				// {
				// 	"appid": "HBuilder",
				// 	"version": "2.0.0",
				// 	"name": "zz-erp",
				// 	"versionCode": "1",
				// 	"description": "",
				// 	"author": "",
				// 	"email": "",
				// 	"features": ["uninview"]
				// }

				uni.request({
					url: getApp().globalData.serverUrl + '/api/init',
					data: JSON.stringify({
						'api_key': getApp().globalData.api_key
					}),
					method: 'POST',
					contentType: 'application/json',
					success: (r) => {
						// r.data.minver = 2.0.0
						// console.log(r.data.minver != inf.version)
						// console.log(cpr_version(r.minver, inf.version))
						// if (r.minver != inf.version && this.cpr_version(r.minver, inf.version)) {
						if (r.data.minver != inf.version) {
							uni.showModal({
								title: "发现新版本",
								content: "请重新下载",
								success: (res) => {
									if (res.confirm == true) {
										plus.runtime.quit(); // 退出应用
									} else {
										plus.runtime.quit(); // 退出应用
									}
								}
							})
						}
					}
				})
			})
			//#endif 
			// setAuthorization(JSON.stringify({"token":123456,"user_id":123456}))
			// routingIntercept()
			
		},
		onShow: function() {
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// // 初始化
			// infoLogin(data) {
			// 	return request({
			// 		url: '/api/init',
			// 		method: 'POST',
			// 		contentType: 'application/json',
			// 		data: JSON.stringify(data)
			// 	})
			// },
			//假定字符串的每节数都在5位以下
			toNum(a) {
				var a = a.toString();
				//也可以这样写 var c=a.split(/\./);
				var c = a.split('.');
				var num_place = ["", "0", "00", "000", "0000"],
					r = num_place.reverse();
				for (var i = 0; i < c.length; i++) {
					var len = c[i].length;
					c[i] = r[len] + c[i];
				}
				var res = c.join('');
				return res;
			},
			// a>b返回true
			cpr_version(a, b) {
				var _a = this.toNum(a),
					_b = this.toNum(b);
				return _a > _b
				// if (_a == _b) console.log("版本号相同！版本号为：" + a);
				// if (_a > _b) console.log("版本号" + a + "是新版本！");
				// if (_a < _b) console.log("版本号" + b + "是新版本！");
			},
			// ...mapMutations(['login'])

		}
	}
</script>

<style>
	/* #ifndef APP-PLUS-NVUE */
	/* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	@import './common/uni.css';

	/* H5 兼容 pc 所需 */
	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	uni-page-body {
		/* background-color: #F5F5F5 !important; */
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}

	/* #endif */

	/* 以下样式用于 hello uni-app 演示所需 */
	/* page {
		background-color: #efeff4;
		height: 100%;
		font-size: 28rpx;
		line-height: 1.8;
	} */

	.fix-pc-padding {
		padding: 0 50px;
	}

	.uni-header-logo {
		padding: 30rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 10rpx;
	}

	.uni-header-image {
		width: 100px;
		height: 100px;
	}

	.uni-hello-text {
		color: #7A7E83;
	}

	.uni-hello-addfile {
		text-align: center;
		line-height: 300rpx;
		background: #FFF;
		padding: 50rpx;
		margin-top: 10px;
		font-size: 38rpx;
		color: #808080;
	}

	/* #endif*/
</style>
